<?php

/**
 * Created by PhpStorm.
 * User: chen
 * Date: 2015/11/4
 * Time: 10:28
 */
class VipServerAction extends Action
{
    //积分变更
    public function pointsChange()
    {   
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $actName = trim(I('get.actName'));//项目名
        $actid = trim(I('get.actid'));//项目id
        $operate = trim(I('get.operate'));//具体积分操作
        $points = trim(I('get.points'));//变动分值
        $time = trim(I('get.time'));//时间
        $sign = trim(I('get.sign'));//token

        if($openid==null){
            $this->re(1,'need openid');
        }

        if($actName==null){
            $this->re(1,'need actName');
        }

        if($actid==null){
            $this->re(1,'need actid');
        }

        if($operate==null||!in_array($operate, array('add','reduce'))){
            $this->re(1,'need operate');
        }

        if($points==null){
            $this->re(1,'need points');
        }

        if($time==null){
            $this->re(1,'need time');
        }else{
            $le=$time-time();
            if($le<-10 || $le>10){
                $this->re(1, 'unable timestamp');
            }
        }

        if (empty($sign)) {
            $this->re(1, 'need sign');
        }

        if ($sign != md5(md5($openid . $points . $time))) {
            $this->re(1, 'wrong sign');
        }



        $map = array();
        $map['id'] = $userid;
        $rs = M('vipList')->where($map)->find();
        if(!$rs){
            $this->re(4,'not vip');
        }
        if($operate=='add'){
            $r = M('vipList')->where($map)->setInc('points',$points);
        }elseif ($operate=='reduce') {
            if($rs['points']<$points){
                $this->re(4,'points less');
            }
            $r = M('vipList')->where($map)->setDec('points',$points);
        }
        $data =array();
        $data['userid'] = $userid;
        $data['operate'] = $operate;
        $data['actName'] = $actName;
        $data['actid'] = $actid;
        $data['score']  = $points;
        $data['createtime'] = time();
        $data['type'] = 'points';
        M('vipRecord')->data($data)->add();
        $this->re(200,'ok');
    }
    //余额变更
    public function balanceChange()
    {
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $actName = trim(I('get.actName'));
        $actid = trim(I('get.actid'));
        $operate = trim(I('get.operate'));
        $balance = trim(I('get.balance'));//余额
        

        if($openid==null){
            $this->re(1,'need openid');
        }

       

        if($actName==null){
            $this->re(1,'need actName');
        }

        if($actid==null){
            $this->re(1,'need actid');
        }

        if($operate==null){
            $this->re(1,'need operate');
        }

        if($balance==null){
            $this->re(1,'need balance');
        }

        $map = array();
        
        $map['id'] = $userid;
        $rs = M('vipList')->where($map)->find();
        if(!$rs){
            $this->re(4,'not find');
        }
        if($operate=='add'){
            M('vipList')->where($map)->setInc('balance',$balance);
            M('vipList')->where($map)->setInc('total',$balance);
            $total = $rs['total']+$balance;
            $m = array();
            $lv = M('vipLevel')->where($m)->order('level desc')->select();
            foreach ($lv as $key => $value) {
                if($total>=$value['consumption']){
                    $level = $value['level'];
                    break;
                }
            }
            M('vipList')->where($map)->setInc('viplevel',$level);


        }elseif ($operate=='reduce') {
            if($rs['balance']<$balance){
                $this->re(4,'balance less');
            }
            M('vipList')->where($map)->setDec('balance',$balance);
        }
        $data =array();
        $data['userid'] = $userid;
        $data['operate'] = $operate;
        $data['actName'] = $actName;
        $data['score']  = $balance;
        $data['createtime'] = time();
        $data['type'] = 'balance';
        M('vipRecord')->data($data)->add();

        $this->re(200,'ok');

        
    }


    //余额查询
    public function getbalance(){
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $map = array();
        $map['id'] = $userid;
        $balance = M('vipList')->where($map)->getField('balance');
        $arr = array();
        $arr['code'] = 200; 
        $arr['msg'] = 'ok'; 
        $arr['balance'] = $balance;
        $this->ajaxReturn($arr); 

    }
    //积分查询
     public function getpoints(){
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $map = array();
        $map['id'] = $userid;
        $points = M('vipList')->where($map)->getField('points');
        $arr = array();
        $arr['code'] = 200; 
        $arr['msg'] = 'ok'; 
        $arr['points'] = $points;
        $this->ajaxReturn($arr); 

    }
    //用户信息获取
    public function getUser(){
        $openid = trim(I('get.openid'));
        $time = trim(I('get.time'));//时间
        $sign = trim(I('get.sign'));//token


        if($time==null){
            $this->re(1,'need time');
        }else{
            $le=$time-time();
            if($le<-10 || $le>10){
                $this->re(1, 'unable timestamp');
            }
        }

        if (empty($sign)) {
            $this->re(1, 'need sign');
        }

        if ($sign != md5(md5($openid . $time))) {
            $this->re(1, 'wrong sign');
        }


        $userid = $this->getIdbyOpenid($openid);//id
        $map = array();
        $map['id'] = $userid;
        $user = M('vipList')->where($map)->find();
        if(!$user){
            $this->re(1,'not vip');
        }
        $arr = array();
        $arr['code'] = '200';
        $arr['msg'] = 'ok';
        $arr['user'] = $user;
        $this->ajaxReturn($arr);
    }
    //会员注册
    public function vipregist(){
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $rs = M('vipList')->where(array('openid'=>$openid))->find();
        if($rs){
            $this->re(1,'already vip');
        }else{
            $vipid = $this->getVipNum();
            $data = array();
            $data['openid'] = $openid;
            $data['vipid'] = $vipid;
            $data['createtime'] = time();
            $r = M('vipList')->data($data)->add();
            if($r){
                $this->re(200,'ok');
            }
        }

    }

    //完善会员信息
    public function replenish(){
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $map = array();
        $map['id'] = $userid;
        $vipid = trim(I('get.vipid'));
        $name = trim(I('get.name'));
        $nickname = trim(I('get.nickname'));
        $mobile = trim(I('get.mobile'));
        $email = trim(I('get.email'));
        $address = trim(I('get.address'));
        $data = array();
        if(!empty($name)){
            $data['name'] = $name;
        }
        if(!empty($nickname)){
            $data['nickname'] = $nickname;
        }
        if(!empty($mobile)){
            $data['mobile'] = $mobile;
        }
        if(!empty($email)){
            $data['email'] = $email;
        }
        if(!empty($address)){
            $data['address'] = $address;
        }
        $rs = M('vipList')->where($map)->data($data)->save();
        if($rs){
            $this->re(200,'ok');
        }else{
            // $this->ajaxReturn($data);
            $this->re(1,'error');
        }
        
        
    }
    //会员线下绑定
    public function bandVip(){
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $map = array();
        $map['id'] = $userid;
        $rs = M('vipList')->where($map)->find();
        if(!$rs){
            $this->re(1,'not vip');
        }
        if($rs['vipid']!=null){
            $this->re(1,'already band');
        }else{
            $r = M('vipList')->where($map)->setField('vipid',$vipid);
        }
    }
    //会员等级查询
    public function getviplevel(){
        $openid = trim(I('get.openid'));
        $userid = $this->getIdbyOpenid($openid);//id
        $map = array();
        $map['id'] = $userid;
        $rs = M('vipList')->where($map)->field('viplevel')->find();
        if(!$rs){
            $this->re(1,'not vip');
        }else{
            $array = array();
            $array['code'] = 200;
            $array['msg'] = 'ok';
            $array['level'] = $rs['viplevel'];
            $this->ajaxReturn($array);
        }

    }

    // public function 

    public function getVipNum(){
        $vipModel = D('vipList');
        do {
            $str = rand_string(16, 2);
            $map = array();
            $map['vipid'] = $str;
            $rs = $vipModel->where($map)->find();
        } while ($rs);  //如果存在，$rs非空，那么就重新获取
        return $str;
    }

    /**
     * ajax返回
     */
    public function re($code,$msg){
        $arr=array();
        $arr['code']=$code;
        $arr['msg']=$msg;
        $this->ajaxReturn($arr);
    }

    public function getIdbyOpenid($openid){
        $id = M('vipList')->where(array('openid'=>$openid))->getField('id');
        if(!$id){
            $this->re(1,'not register');
        }
        return $id;
    }

    public function httpget($url)
    {
        $ch = curl_init();
        //设置选项，包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //执行并获取HTML文档内容
        $output = curl_exec($ch);
        //释放curl句柄
        curl_close($ch);
        return $output;
    }
}